// 获取搜索框value
$('.searchText button').on({
    click() {
        console.log($('.searchText>input').val());
        $('.searchText>a').attr('href', `search.html?keyword=${$('.searchText>input').val()}&subjectId=${obj.subjectId}&type=${obj.type}&courseType=${obj.courseType}`);
    }
})
// let key = obj.keyword
let key = decodeURI(obj.keyword)
$('.searchText>input').val(key)

// 获取网址后面参数
// 课程搜索接口
var pageNum = 1;
var data = {
    pageNum: 1,
    pageSize: 10,
    ...obj
}
let total;
let pageNumMax;
// 课程搜索接口

if (obj.keyword == "") {
    classSearch('http://wkt.myhope365.com//pc/course/search/keyword');
} else {
    classSearch(`http://wkt.myhope365.com//weChat/applet/course/search/keyword/${obj.keyword}`);
}
function classSearch(url) {
    request(url, type = "get", data).then((res) => {
        total = res.total
        pageNumMax = Math.ceil(total / 10);
        $('.pagination').empty()
        $(".pagination").append(`<li class="first">&laquo;</li>`)
        for (let i = pageNumMax; i > 0; i--) {
            $(".pagination>li:first-child").after(`<li class = "page">${i}</li>`);
        }
        $.each($('.page'), function (index, value) {
            console.log(value);
            console.log(this);
            $(this).on(
                {
                    mouseenter(){
                        $(this).css({ "border-color": "#00cf8c", "color": "#00cf8c" }).siblings('.page').css({ "border-color": "#aaa", "color": "black" })
                        console.log(this);

                    }
                }
            )
        })
        // console.log($('.pagination').children().length - 1);
        if ($('.pagination').children().length - 1 == 0) {
            $('.classList').append(`<div style="text-align:center;font-size : 18px;padding-top:30px">暂无相关搜索结果，换个关键词试试吧 : )</div>`)
            $('.pagination').css('display', 'none')
        }
        $(".pagination").append(`<li class="last" style="margin-right:0">&raquo;</li>`)
        if ($('.pagination').children().length - 2 == 1) {
            $('.first').css({ "cursor": "not-allowed", "border-color": "#ccc", "color": "#ccc" })
            $('.last').css({ "cursor": "not-allowed", "border-color": "#ccc", "color": "#ccc" })
        }
        $('.page').click(function () {
            pageNum = $(this).text();
            data = {
                pageNum: pageNum,
                pageSize: 10,
                ...obj
            }
            $('.classList>ul').empty()
            if (obj.keyword == "") {
                classSearch('http://wkt.myhope365.com//pc/course/search/keyword');
            } else {
                classSearch(`http://wkt.myhope365.com//weChat/applet/course/search/keyword/${obj.keyword}`);
            }

        })

        console.log(pageNum);
        $.each($('.page'), function (index, value) {
            if ($(value).text() == pageNum) {
                $(this).css('border-color', '#23db9b').siblings().css('border-color', '#aaa')
                $(this).css('color', '#23db9b').siblings().css('color', 'black');
            }
        })

        $('.first').click(function () {
            if (pageNum > 1) {
                pageNum--;
                data = {
                    pageNum: pageNum,
                    pageSize: 10,
                    ...obj
                }
                $('.classList>ul').empty()
                if (obj.keyword == "") {
                    classSearch('http://wkt.myhope365.com//pc/course/search/keyword');
                } else {
                    classSearch(`http://wkt.myhope365.com//weChat/applet/course/search/keyword/${obj.keyword}`);
                }
            }

        })

        $('.last').click(function () {
            if (pageNum < pageNumMax) {
                pageNum++;
                data = {
                    pageNum: pageNum,
                    pageSize: 10,
                    ...obj
                }

                $('.classList>ul').empty()
                if (obj.keyword == "") {
                    classSearch('http://wkt.myhope365.com//pc/course/search/keyword');
                } else {
                    classSearch(`http://wkt.myhope365.com//weChat/applet/course/search/keyword/${obj.keyword}`);
                }
            }

        })

        console.log(`pageNum的值为${pageNum}`);
        if (pageNum == 1) {
            $('.first').css({ "cursor": "not-allowed", "border-color": "#ccc", "color": "#ccc" })
            // $('.last').css({ "cursor": "not-allowed", "border-color": "#ccc", "color": "#ccc" })
        }
        if (pageNum == pageNumMax) {
            // $('.last').prop("disabled", true)
            // $('.last').css("cursor", "not-allowed")
            // $('.first').css({ "cursor": "not-allowed", "border-color": "#ccc", "color": "#ccc" })
            $('.last').css({ "cursor": "not-allowed", "border-color": "#ccc", "color": "#ccc" })
        }
        $.each(res.courseList, function (index, value) {
            if (value.isFree == 1) {
                price = '免费'
            } else {
                price = `<span style="color:red">￥${value.discountPrice}</span><span style="color:#999;font-size:12px;text-decoration: line-through;margin-left:4px">￥${value.coursePrice}</span>`
            }
            if (value.isDiscount == 1) {
                $('.classList>ul>li:last-child').append(`
                    <a href="detail.html?keyword=&id=${value.courseId}&subjectId=&type=&courseType=" class="discount">
                        ${value.discountDesc}
                    </a>
                `);
            }
            $('.classList>ul').append(`
                <li>
                    <a href="detail.html?keyword=&id=${value.courseId}&subjectId=&type=&courseType=" target="_blank">
                        <img src="${value.coverFileUrl}" alt="">
                    </a>
                    <a href="detail.html?keyword=&id=${value.courseId}&subjectId=&type=&courseType=" target="_blank">${value.courseTitle}</a>
                    <span>共${value.subSectionNum}节课 | ${value.learningNum}人报名</span><br><span>${price}</span>
                </li>
            `);
        })
    })
}




